/**
 * 经纬度构成的范围
 */
import {LatLng} from "./LatLng";
import {CRS} from "../crs/CRS";
import {MapCamera} from "../render/MapCamera";

export class LatLngBounds {
    //左下,西南
    southWest: LatLng;
    //右上,东北
    northEast: LatLng;

    constructor(southWest: LatLng, northEast: LatLng) {
        this.southWest = southWest;
        this.northEast = northEast;
    }

    static createForCoordinates(coordinates: Array<number>):LatLngBounds {
        return new LatLngBounds(new LatLng(coordinates[1],coordinates[0]),
            new LatLng(coordinates[3],coordinates[2]));
    }

    //西北,左上
    getNorthWest(): LatLng {
        return new LatLng(this.getNorth(), this.getWest());
    }

    //东南,右下
    getSouthEast(): LatLng {
        return new LatLng(this.getSouth(), this.getEast());
    }

    getWest(): number {
        return this.southWest.longitude;
    }

    getSouth(): number {
        return this.southWest.latitude;
    }

    getEast(): number {
        return this.northEast.longitude;
    }

    getNorth(): number {
        return this.northEast.latitude;
    }

    public getCenter(): LatLng {
        let lat = (this.southWest.latitude + this.northEast.latitude) / 2;
        let lon = (this.southWest.longitude + this.northEast.longitude) / 2;
        return new LatLng(lat, lon);
    }

    public getWidth(camera: MapCamera): number {
        let projectSW = camera.projection(this.southWest);
        let projectNE = camera.projection(this.northEast);
        return projectNE.x - projectSW.x;
    }

    public getHeight(camera: MapCamera): number {
        let projectSW = camera.projection(this.southWest);
        let projectNE = camera.projection(this.northEast);
        return projectNE.y - projectSW.y;
    }

    public getScreenWidth(camera: MapCamera): number {
        let projectNW = camera.LatLngToScreenPoint(this.getNorthWest());
        let projectSE = camera.LatLngToScreenPoint(this.getSouthEast());
        return projectSE.x - projectNW.x;
    }

    public getScreenHeight(camera: MapCamera): number {
        let projectNW = camera.LatLngToScreenPoint(this.getNorthWest());
        let projectSE = camera.LatLngToScreenPoint(this.getSouthEast());
        return projectSE.y - projectNW.y;
    }
}
